#include "ZigbeeConfigProtocol.h"

ZigbeeConfigProtocol::ZigbeeConfigProtocol(QObject *parent) :
    BaseProtocol(parent)
{
    this->m_protocolType = ProtocolType::ZigbeeConfigProtocol;
}

QList<Package> ZigbeeConfigProtocol::parseData(QByteArray data)
{
    quint8  checkSteps = CHECK_HEAD;          //当前解析状态
    quint16 lengthCount;                 //数据长度计算
    quint8  headNum=0;                    //数据头索引计算
    quint16 bodyLength;
    QByteArray dataArray;
    QList<Package> packageList;

    for(int i=0; i<data.size(); i++)
    {
        quint8 byte = data.at(i);
        switch(checkSteps)
        {
            case CHECK_HEAD:   //检查数据头
                 if(byte == 0xA5 && headNum == 0)
                 {
                     headNum++;
                 }
                 if(byte == 0xAA && headNum == 1)
                 {
                     headNum++;
                 }
                 if(byte == 0x5A && headNum == 2)
                 {
                    headNum=0;
                    lengthCount = 0;
                    bodyLength = 0;
                    dataArray.clear();
                    dataArray.append(0xA5);
                    dataArray.append(0xAA);
                    dataArray.append(0x5A);
                    checkSteps = CHECK_FUNCTION;
                 }
            break;

            case CHECK_FUNCTION:   //接收数据功能
                 dataArray.append(byte);
                 switch(byte)
                 {
                    case ZigbeeConfigProtocol::QueryNewZigbeeParam:     //设置Zigbee参数命令长度为1字节
                         bodyLength = 4;
                         break;
                    case ZigbeeConfigProtocol::SetZigbeeParam:     //设置Zigbee参数命令长度为1字节
                         bodyLength = 1;
                         break;
                    case ZigbeeConfigProtocol::GetZigbeeParam:     //获取Zigbee参数命令长度为31字节
                         bodyLength = 31;
                         break;
                    case ZigbeeConfigProtocol::ReStartZigbee:       //复位Zigbee命令长度为1字节
                         bodyLength = 1;
                         break;
                    case ZigbeeConfigProtocol::QueryZigbeeVersion:  //查询Zigbee版本号长度为1字节
                         qDebug()<<"Get QueryZigbeeVersionCmd";
                         bodyLength = 1;
                         break;
                 }
                 checkSteps = CHECK_BODY;
            break;


            case CHECK_BODY:    //接收数据
                 lengthCount++;
                 dataArray.append(byte);
                 if(lengthCount == bodyLength)
                 {
                     qDebug()<<"bodyLength right-packagebody";
                     lengthCount = 0;
                     Package package;
                     package.setHead(dataArray.mid(0,3));
                     package.setFunction(dataArray.mid(3,1).toHex().toInt(0,16));
                     package.setLength(bodyLength);
                     package.setBody(dataArray.mid(4,bodyLength));
                     packageList.append(package);

                     checkSteps = CHECK_HEAD;
                 }
                 else
                 {
                     checkSteps = CHECK_BODY;
                 }
                 break;
        }
    }
    qDebug()<<"zigbeeconfigprotocol return";
    return packageList;
}

QByteArray ZigbeeConfigProtocol::generateData(Package package)
{
    QByteArray head = QByteArray::fromHex(QString("A5AA5A").toLatin1());
    QByteArray data;
    data.append(head);
    data.append(DataTypeConver::intToByteArray(package.function(),1));
    data.append(package.body());
    return data;
}
